package org.smi.answer;

import com.alibaba.fastjson2.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class DoJSON {


    public static void main(String[] args) {
        DoJSON doJSON = new DoJSON();

        String fileContent = doJSON.readFile("/Users/beyondsoft/Documents/数字人合作伙伴培训考试（后端卷）-202408.txt");

        fileContent = fileContent.replace("\\n", "");
        List<AnswerRes> answerResList = doJSON.convertJSON(fileContent);

        for (int i = 0; i < answerResList.size(); i++) {
            System.out.println(i + ":" + answerResList.get(i).getQuestion());
            System.out.println("答案：");
//            for (String str : answerResList.get(i).getAnswerList()) {
//                System.out.println(str);
//            }
            for (AnswerRes.AnswerEntity answer : answerResList.get(i).getAnswerList()) {
                System.out.println(answer.getPosition() + "、" + answer.getAnswer() + answer.getExt());
            }
            System.out.println("------------------------------");
        }
    }

    public String readFile(String dir) {
        StringBuffer retStr = new StringBuffer("");
        try {
            FileReader fileReader = new FileReader(dir);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                retStr.append(line);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return retStr.toString();
    }

    public List<AnswerRes> convertJSON(String json) {
        AnswerModule answerModule = JSONObject.parseObject(json, AnswerModule.class);

        List<AnswerModule.Question> listQuestion = answerModule.getData().getQuestions();

        List<AnswerRes> answerResList = new ArrayList<>();
        for (AnswerModule.Question s : listQuestion) {
            AnswerRes answerRes = new AnswerRes();

            AnswerModule.Content askContent = s.getContent();
            String ask = askContent.getSubject().getTxt();
            answerRes.setQuestion(ask);
            AnswerModule.Data2 data = askContent.getData();

            // 多选
            AnswerModule.Multiple multiple = data.getMultiple();
            if (multiple != null) {
                List<AnswerModule.Option> optionList = multiple.getOptions();
//                optionList.sort((o1, o2) -> {
//                    return o1.getPosition() - o2.getPosition();
//                });
//
//                List<String> answerTxt = new ArrayList<>();
//
//                List<Integer> answerP = multiple.getAnswer();
//                for (int i = 0; i < optionList.size(); i++) {
//                    if (answerP.contains(i)) {
//                        answerTxt.add(optionList.get(i).getTxt());
//                    }
//                }
//                answerRes.setAnswer(answerTxt);



                List<AnswerRes.AnswerEntity> answerEntityList = new ArrayList<>();

                List<Integer> answerP = multiple.getAnswer();
                for (int i = 0; i < optionList.size(); i++) {
                    AnswerRes.AnswerEntity answer = new AnswerRes.AnswerEntity();
                    answer.setAnswer(optionList.get(i).getTxt());
                    answer.setPosition(optionList.get(i).getPosition());

                    if (answerP.contains(i)) {
                        answer.setExt(" [***]");
                    } else {
                        answer.setExt("");
                    }
                    answerEntityList.add(answer);
                }
                answerRes.setAnswerList(answerEntityList);
            }

            // 单选
            AnswerModule.Single single = data.getSingle();
            if (single != null) {
                Integer answerP = single.getAnswer();
                List<AnswerModule.Option> optionList = single.getOptions();
                List<AnswerRes.AnswerEntity> answerEntityList = new ArrayList<>();
                for (int i = 0; i < optionList.size(); i++) {
                    AnswerRes.AnswerEntity answer = new AnswerRes.AnswerEntity();
                    answer.setAnswer(optionList.get(i).getTxt());
                    answer.setPosition(optionList.get(i).getPosition());

                    if (answerP == i) {
                        answer.setExt(" [***]");
                    } else {
                        answer.setExt("");
                    }

                    answerEntityList.add(answer);
                }
                answerRes.setAnswerList(answerEntityList);
            }
            answerResList.add(answerRes);
        }

        return this.sort(answerResList);

//        return answerResList;
    }

    private static final String title1 = "【";

    private List<AnswerRes> sort(List<AnswerRes> answerResList) {
        List<AnswerRes> list1 = new ArrayList<>();
        List<AnswerRes> list2 = new ArrayList<>();

        for (AnswerRes answerRes : answerResList) {
            List<AnswerRes.AnswerEntity> answerEntityList = answerRes.getAnswerList();
            answerEntityList.sort((c1, c2) -> {
                return c1.getPosition() - c2.getPosition();
            });

            if (answerRes.getQuestion().startsWith(title1)) {
                list1.add(answerRes);
            }
        }

        list1.addAll(list2);
        return list1;
    }

    private List<AnswerRes> genList() {
        List<AnswerRes> list1 = new ArrayList<>();
        List<AnswerRes> list2 = new ArrayList<>();

        return null;
    }
}
